#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int HASHLEN; 

struct hash_el_ 
{
    struct hash_el_ *nxt;
};

struct hash 
{
	int numelements;
	long int userid;
        char password[10];
    	struct hash_el_ *head;
    	struct hash_el_ *tail;
};

int h1(int k)
{
    return (k)%HASHLEN;
}

/*int h2(int k)
{
	return (HASHLEN*0618*k)%1
}*/


struct hash *init_table(void)
{
    int i = 0;
    struct hash *node;
    node = malloc(sizeof(*node) * HASHLEN);
    if (node == NULL) 
    {
    	return NULL;
    }

    for (i = 0; i < HASHLEN; i++) 
    {
    node[i].numelements = 0;
    node[i].userid = 0;
    strncpy(node[i].password, "", sizeof(node[i].password));
    node[i].head = NULL;
    node[i].tail = NULL;
    }
    return node;
}

struct hash_el_ *append_table(struct hash * h, int k,int int_str1,char str2[])
{
    int p = 0, hval = 0;
    struct hash_el_ *new, *pt;

    hval = h1(k);
    if (h[hval].numelements == 0) 
    {
    	if ((new = malloc(sizeof(new))) == NULL) 
        {  
        	return NULL;
        }      
	h[hval].head = new;
        h[hval].tail = new;
        new->nxt = NULL;
	h[hval].userid=int_str1;
	//h[hval].password=str2;  
        h[hval].numelements++;
        return new;
    } 
    else 
    {
    	if ((new = malloc(sizeof(new))) == NULL) 
	{
	        return NULL;
        }
        new->nxt = NULL;
        h[hval].tail->nxt = new;
        h[hval].tail = new;
	h[hval].userid=int_str1;
	//h[hval].password=str2;  
        h[hval].numelements++;
        return new;
    }
}

void append(char s[], char c)
{
        int len = strlen(s);
        s[len] = c;
        s[len+1] = '\0';
}


int hashlen_calc()
{
	int i;
	char c,cc;
	FILE *f;
	i=0; //to count number of line
	f=fopen("/home/vasant/hash","r"); //fopen("file name","mode i.e r-read");
	while((c=getc(f))!=EOF) //loop for reading a char upto end of file
	{
		i++;
	}
	fclose(f);
	return i;
}

void input()
{
	int i,j,k,t;
	int uid;
	char pass[10];
	t=4;
	for(i=0;i<3;i++)
	{	
		printf("\n Enter user id : ");
		scanf("%d",&uid);
		printf("\n Enter password :");
		scanf("%s",pass);
		j=0;
		while(j<HASHLEN)
		{
			k=h1(uid);
			if(j==k)
			{
				printf("\n CORRECT PASSWORD ");
			}
			j=j+1;
		}
		t=t-1;
		print("\nYou have %d trials left,try again",t);
	}
	printf("Incorrect password");
}


void main()
{
	struct hash *node;	
	char str[30],str1[30],str2[30];		
	int i,j,int_str1,k;
	k=0;
	char c,cc,ch;
	HASHLEN=hashlen_calc();
	FILE *f;
	f=fopen("/home/vasant/hash","r"); //fopen("file name","mode i.e r-read");
	while((c=getc(f))!=EOF)//loop for reading a char upto end of file
	{
		strncpy(str, "", sizeof(str));		
		strncpy(str1, "", sizeof(str1));	
		strncpy(str2, "", sizeof(str2));	
		while((cc=getc(f))!='\n') // loop for reading a char upto end of line
		{				
			putchar(cc); // to display line
			append(str,cc);		
			
		}
		printf("\n%s\n",str);
		i=0;
		while(str[i]!=' ')
		{
			append(str1,str[i]);
			i=i+1;
		}
		printf("\n%s\n",str1);
		int_str1=atoi(str1);
		j=i+1;
		while(str[j]!='\0')
		{
			append(str2,str[j]);
			j=j+1;
		}
		printf("\n%s\n",str2);
		//k=h1(int_str1);
		append_table(node,int_str1,int_str1,str2);
		//node[k].userid=int_str1;
		//node[k].password=str2;
		printf("\n");
		input();
	}
	fclose(f);
}





